package com.medic.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.medic.entity.Reply;
import com.medic.entity.Topic;
import com.medic.mapper.interf.TopicMapper;
import com.medic.service.interf.TopicServiceInterface;
import com.medic.util.CacheUtil;


@Service("topicService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class TopicServiceImpl implements TopicServiceInterface {

	@Resource
	private TopicMapper topicMapper;


	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page<Topic> getTopicByUser(int pageno, int userid, int groupid) throws Exception {
		// 分页准备
		
		PageHelper.startPage(pageno, CacheUtil.getTopic_pagesize());
		PageHelper.orderBy("noreadcount desc");

		Page<Topic> page = (Page<Topic>) topicMapper.selectTopicByUserid(userid,groupid);

		return page;

	}


	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page<Topic> getTopicByMedic(int pageno, int groupid) throws Exception {

		PageHelper.startPage(pageno, CacheUtil.getTopic_pagesize());
		PageHelper.orderBy("sort asc,addtional desc");
 
		Page<Topic> page = (Page<Topic>) topicMapper.selectTopicByMedic(groupid);

		return page;

	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page<Reply> otherGetReplyBy(int pageno, int topicid) throws Exception {

		PageHelper.startPage(pageno,CacheUtil.getReply_pagesize());
		// 按时间升序
		// PageHelper.orderBy("sort asc");

		Page<Reply> page = (Page<Reply>) topicMapper.selectReplyByTopicid(topicid);

		return page;
	}

	@Override
	public void otherReplyTo(Reply reply) throws Exception {

		topicMapper.insertReply(reply);

		topicMapper.updateForOtherReply(reply.getTopicid());

	}

	@Override
	public Page<Reply> userGetReplyBy(int pageno, int topicid) throws Exception {

		PageHelper.startPage(pageno, CacheUtil.getReply_pagesize());
		// 按时间升序 
		// PageHelper.orderBy("sort asc");

		Page<Reply> page = (Page<Reply>) topicMapper.selectReplyByTopicid(topicid);

		 
		topicMapper.updateForUserGetReply(topicid);

		return page;
	}

	@Override
	public void userReplyTo(Reply reply) throws Exception {
		topicMapper.insertReply(reply);

		topicMapper.updateForUserReply(System.currentTimeMillis(),reply.getTopicid());

	}
	@Deprecated
	@Override
	public boolean update(Topic object) throws Exception {
		topicMapper.update(object);
		return true;
	}
	
	
	
	public Topic getTopicById(int topicid) throws Exception{
		
		return topicMapper.selectTopicById(topicid);
		
	}
	
	@Override
	@Deprecated
	public boolean delete(Topic object) throws Exception {
		topicMapper.delete(object);
		return true;
	}

	@Override
	public boolean save(Topic object) throws Exception {
		topicMapper.save(object);
		return true;
	}


}
